package com.apobates.forum.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 提示消息
 * 
 * @author xiaofanku@live.cn
 * @since 20170511
 */
public final class TipMessage {
	/**
	 * 消息内容
	 */
	private final String message;
	/**
	 * 消息类型
	 */
	private final String level;
	/**
	 * 绑定的输入元素name
	 */
	private final String input;

	public TipMessage(String message) {
		this.level = TipMessageLevelEnum.ERR.getLevel();
		this.message = message;
		this.input = null;
	}

	public TipMessage(String message, TipMessageLevelEnum level) {
		this.message = message;
		this.level = level.getLevel();
		this.input = null;
	}
	
	public TipMessage(String message, TipMessageLevelEnum level, String input) {
		this.message = message;
		this.level = level.getLevel();
		this.input = input;
	}
	
	public TipMessage(String message, String level) {
		this.message = message;
		this.level = level;
		this.input = null;
	}

	public TipMessage(String message, String level, String input) {
		this.message = message;
		this.level = level;
		this.input = input;
	}

	// GET/SET
	public String getMessage() {
		return message;
	}

	public String getLevel() {
		return level;
	}

	public String getInput() {
		return input;
	}

	public String toJsonString() {
		return Commons.toJson(this);
	}
	
	public String toJsonString(Map<String,String> extOutputParames) {
		Map<String,String> data = toMap();
		data.putAll(extOutputParames);
		return Commons.toJson(data);
	}
	
	public Map<String,String> toMap(){
		Map<String,String> data = new HashMap<>();
		data.put("message", getMessage());
		data.put("level", getLevel());
		data.put("input", getInput());
		return data;
	}
	
	public static TipMessage ofError(String message) {
		return new TipMessage(message);
	}

	public static TipMessage ofSuccess(String message) {
		return new TipMessage(message, TipMessageLevelEnum.ACC);
	}
	
	public static TipMessage ofSuccess(String message, String url) {
		return new TipMessage(message, TipMessageLevelEnum.ACC, url);
	}
}
